package com.cheyun.netsalev3.viewmodel

import android.os.Looper
import android.util.Log
import android.view.View
import android.widget.TimePicker
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import com.cheyun.netsalev3.bean.DialogParam
import com.cheyun.netsalev3.bean.YaoYueEditItem
import com.cheyun.netsalev3.repository.ReceptionRepository
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.widget.MyDialog
import com.cheyun.netsalev3.widget.TimePickerFragment
import com.jeremyliao.liveeventbus.LiveEventBus
import java.util.*

class ReceptionActivityViewModel(var id: String, var receptionRepository: ReceptionRepository) : BaseViewModel() {
    var data = ObservableField<YaoYueEditItem>()

    var content: AppCompatActivity? = null
    //到店时间
    var temData = ""
    var showTime = ObservableField<String>()
    // 接待人
    var showJidai = ObservableField<DialogParam>()

    //到店人数
    var peopleNum = ""
    var year = 0
    var month = 0
    var day = 0

    init {
        val c = Calendar.getInstance()
        year = c.get(Calendar.YEAR)
        month = c.get(Calendar.MONTH) + 1
        day = c.get(Calendar.DAY_OF_MONTH)
        temData = FunctionUtils.getTime()
        showTime.set(temData)
        receptionRepository.getInviteEdit(id, this::huiDiao)
    }

    fun huiDiao(item: YaoYueEditItem) {
        Log.e(":item", item.toString())
        data.set(item)
    }

    /**
     * 到店时间
     */
    fun comeData(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            this.content = content
            var timeDialog = TimePickerFragment()
            timeDialog.clickOk = this::timeHuiDiao
            timeDialog.show(content.supportFragmentManager, "")
        }
    }


    fun timeHuiDiao(view: TimePicker, hourOfDay: Int, minute: Int) {
        temData = FunctionUtils.formatTime(year.toString(), month.toString(), day.toString(), hourOfDay, minute)
        showTime.set(temData)
    }

    var temlist = ArrayList<DialogParam>()

    init {
        MySharedPreferences.getBaseData()?.member?.apply {
            for (item in this) {
                var item = DialogParam(item.realname, item.uid.toString())
                temlist.add(item)
            }
        }
    }

    /**
     * 接待人
     */
    fun onClickJidai(view: View) {
        var content = view.context
        if (content is AppCompatActivity) {
            val newFragment = MyDialog.newInstance("选择接待人", temlist)
            newFragment.huidia = this::huiDiaoItem
            newFragment.show(content.supportFragmentManager, "missiles")
        }
    }

    /**
     * 弹窗回调
     */
    fun huiDiaoItem(item: DialogParam?) {
        showJidai.set(item)
    }

    override fun onCleared() {
        super.onCleared()
        content = null
    }

    /**
     * 确定到店
     */
    fun onClickBut(view: View) {
        if (showJidai.get() == null) {
           MyToast("请选择接待人")
            return
        }

        if (peopleNum.isNullOrBlank()) {
           MyToast("请输入到店人数")
            return
        }

        content = view.context as AppCompatActivity

        receptionRepository.update(id, temData, peopleNum.toInt(), showJidai.get()!!.id.toInt(), this::callback)

    }

    /**
     * 确定成功后回调
     */
    fun callback() {
        Looper.prepare()
        Toast.makeText(content, "确定成功", Toast.LENGTH_LONG).show()
        LiveEventBus.get().with("ReceptionActivity").postValue("ReceptionActivity")
        content?.finish()
        Looper.loop()

    }
}
